package org.example.rental.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.rental.common.Result;
import org.example.rental.common.User;
import org.example.rental.domain.SysUser;
import org.example.rental.domain.vo.UserVo;
import org.example.rental.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;

@RestController
@RequestMapping("/user")
public class SysUserController extends BaseController {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    User user;

    @GetMapping("/info/{id}")
    public Result info(@PathVariable(name = "id") Long id){
        SysUser user = sysUserService.getById(id);

        return Result.succ(user);
    }


    @GetMapping("/list")
    public Result list(String username,String phone,Integer status){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(username),"username",username)
                .like(StrUtil.isNotBlank(phone),"phone",phone);
        if (status!=null){
            System.out.println("aa");
            queryWrapper.eq("status",status);
        }

        Page<SysUser> page = sysUserService.page(getPage(),queryWrapper);

        return Result.succ(page);
    }




    @PostMapping("/save")
    public Result save(@Validated @RequestBody SysUser sysUser){

        sysUserService.save(sysUser);
        return Result.succ(sysUser);
    }


    @PostMapping("/update")
    public Result update(@Validated @RequestBody SysUser sysUser){
        sysUserService.updateById(sysUser);
        return Result.succ(sysUser);
    }


    @PostMapping("/delete")
    @Transactional
    public Result delete(@RequestBody Long[] ids){
        //删除
        sysUserService.removeByIds(Arrays.asList(ids));


        return Result.succ("用户删除成功");

    }


    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        SysUser sysUser = user.getSysUser();

        return Result.succ(sysUser);
    }


    @PostMapping("/saveUserInfo")
    public Result saveUserInfo(@RequestBody UserVo userVo){
        sysUserService.saveUserInfo(userVo);
        return Result.succ(200,"更新成功",null);
    }




}
